package uk.org.landeg.euler.problems;

import java.util.Arrays;
import java.util.List;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;
import uk.org.landeg.euler.problems.lib.NumbersLib;
import uk.org.landeg.euler.problems.lib.PrimeLib;

public class Problem049 extends AbstractProblem {
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem049());
		launcher.solve();
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return "Find arithmetic sequences, made of prime terms, whose four digits are permutations of each other.";
	}

	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 49;
	}

	@Override
	public Object solve() {
		notes.add("Generating primes");
		List<Long> primes = PrimeLib.seqPrimes(10000L);
		notes.add("Found " + primes.size());
		int start;
		int test1, test2;

		int count0[], count1[], count2[];

		for (start = 0; start < primes.size(); start++) {
			if (primes.get(start) < 1000) {
				continue;
			}
			count0 = NumbersLib.digitCount(primes.get(start).intValue());
			for (test1 = start + 1; test1 < primes.size(); test1++) {
				count1 = NumbersLib.digitCount(primes.get(test1).intValue());
				if (Arrays.equals(count0, count1)) {
					for (test2 = test1 + 1; test2 < primes.size(); test2++) {
						count2 = NumbersLib.digitCount(primes.get(test2).intValue());
						if (primes.get(test2) - primes.get(test1) == primes.get(test1) - primes.get(start)) {
							if (Arrays.equals(count0, count2)) {
								System.out.println(primes.get(start) + "\t" + primes.get(test1) + "\t" + primes.get(test2));
							}
						}
					}
				}
			}
		}
		return 0;
	}
}
